import base64
import sys
import time
import urllib

import requests
from selenium import webdriver
from selenium.webdriver.chrome.options import Options


def createImage(data):
    print(data)
    chrome_options = Options()
    chrome_options.add_argument('--headless')
    chrome_options.add_argument('--no-sandbox')
    chrome_options.add_argument('--disable-dev-shm-usage')
    d = webdriver.Chrome('/usr/bin/chromedriver', chrome_options=chrome_options)

    d.get('file:///'+sys.path[0]+'/createImage/index.html?data='+data)
    # print('file:///'+sys.path[0]+'/createImage/index.html')

    time.sleep(5)

    src = d.find_element_by_id("showImage").find_elements_by_tag_name('img')[0].get_attribute('src')
    # print(src)
    url = src[22:]
    url_b64 = base64.b64decode(url)

    # print(src)

    with open('./static/classTable.png', 'wb') as file:
        file.write(url_b64)

    d.close()
